<template xmlns="http://www.w3.org/1999/html">
  <sea-data-table ref="tableObject" url="/sys/dic/list" :where="object" :auto-load="false" :is-page="false">
    <!--表头-->
    <sea-table-header slot="header" disabled-form>

      <!--标题栏-->
      <sea-table-title slot="title" title="数据字典" subtitle="规划：添加时，增加一行，允许行内编辑！"></sea-table-title>

      <!--按钮栏-->
      <template slot="buttons">
        <el-button plain size="small" title="新增" icon="fa fa-plus" @click="onAction('new')"></el-button>
      </template>
    </sea-table-header>

    <!--表头-->
    <template slot="columns">
      <el-table-column property="id" label="ID" width="200"></el-table-column>
      <sea-col-input property="valueType" label="字段类型" width="200" :change="onAction"></sea-col-input>
      <sea-col-input property="label" label="Label" width="200" :change="onAction"></sea-col-input>
      <sea-col-input property="value" label="Value" width="200" :change="onAction"></sea-col-input>
      <sea-col-input property="style" label="样式" width="200" :change="onAction"></sea-col-input>
      <sea-col-input property="orderBy" label="排序" :change="onAction"></sea-col-input>

      <!--操作栏-->
      <sea-table-actionbar width="120" :on-action="onAction" :buttons="buttons"></sea-table-actionbar>
    </template>
  </sea-data-table>
</template>

<script>

export default {
    components: {}
    , props: {
        // 成功回调，通常是刷新数据表格
        object: {type: Object, default: undefined}
    }
    , data() {
        return {
            // 操作栏
            buttons: [
                {type: 'btn', evt: 'del', label: '删除', title: '删除当前行'}
            ]
            // end--
        }
    }
    , methods: {
        /**
         * 操作成功回调
         */
        onSucceed: function () {
            this.$refs.tableObject.reload();
        },
        /**
         * 操作栏事件分发
         * @param evt 事件
         * @param row 行级对象
         * @param old 可供回滚的参数
         */
        onAction: function (evt, row, old) {
            let params = {
                schemaName: this.object.schemaName
                , tableName: this.object.tableName
                , columnName: this.object.columnName
            };

            if (evt === 'new') {
                this.$layers.ajax({
                    url: '/sys/dic/info',
                    method: 'POST',
                    params: params
                }).then(ret => {
                    if (ret.code === 0) {
                        Layers.warn("已经增加一行，直接点击单元格进行编辑!");
                        this.$refs.tableObject.reload();
                    }
                })
            } else if (evt === 'del') {
                this.$layers.ajax({
                    method: "DELETE",
                    url: 'sys/dic/info',
                    params: {id: row.id}
                }).then(result => {
                    if (result.code === 0) {
                        this.$layers.succeed();
                        this.$refs.tableObject.reload();
                    }
                });
            } else if (evt === '$edit') {
                this.$layers.ajax({
                    method: "PUT",
                    url: 'sys/dic/info',
                    params: row
                }).then(result => {
                    if (result.code === 0) {
                        this.$layers.succeed();
                    }
                });
            }
        }
    }
}
</script>
